package softtech.apps.callblocker.adapter;

import java.util.ArrayList;
import java.util.List;

import softtech.apps.utility.ControlPhoneAction;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.BlackListItem;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.Setting;
import softtech.apps.callblocker.quickAction.ActionItem1;
import softtech.apps.callblocker.quickAction.QuickAction1;
import softtech.apps.callblocker.quickAction.QuickAction1.OnActionItem1ClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdapterExceptionItem extends BaseAdapter {

	private DatabaseHanler db;
	private Activity activity;
	private List<BlackListItem> blackListItem = new ArrayList<BlackListItem>();
	QuickAction1 mQuickAction;

	public AdapterExceptionItem(Activity activity, List<BlackListItem> list) {
		this.activity = activity;
		this.blackListItem = list;
		db = new DatabaseHanler(activity);
	}

	@Override
	public int getCount() {
		if (blackListItem == null || blackListItem.equals(null)
				|| blackListItem.size() == 0) {
			return 1;
		}
		return blackListItem.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (blackListItem == null || blackListItem.equals(null)
				|| blackListItem.size() == 0) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,
					parent, false);
			return convertView;
		}

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_blacklist, parent,
					false);
		}

		final BlackListItem item = blackListItem.get(position);

		TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
		TextView tvPhoneNumber = (TextView) convertView
				.findViewById(R.id.tvPhoneNumber);
		ImageView img = (ImageView) convertView.findViewById(R.id.imgView);
		LinearLayout linearShowAction = (LinearLayout) convertView.findViewById(R.id.linearShowAction);
		linearShowAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showQuickAction( activity, v.findViewById(R.id.imgView) , item );
			}
		});

		tvName.setText(item.getName());
		tvPhoneNumber.setText(item.getPhoneNumber());

		if (item.getName().equals(null) || item.getName().equals("")) {
			tvName.setVisibility(View.GONE);
			tvPhoneNumber.setGravity(Gravity.CENTER_VERTICAL);
		} else {
			tvName.setVisibility(View.VISIBLE);
			tvName.setText(item.getName());
			tvPhoneNumber.setText(item.getPhoneNumber());
			tvPhoneNumber.setGravity(Gravity.NO_GRAVITY);
		}

		return convertView;
	}

	static class ViewHolder {
		ImageView imgCallMode;
		ImageView img;
		TextView tvName;
		TextView tvPhoneNumber;
		LinearLayout linearShowAction;
	}

	void showQuickAction(Context context, View view , final BlackListItem item) {
		ActionItem1 item1 = new ActionItem1(0, context.getResources().getString(R.string.call), context
				.getResources().getDrawable(R.drawable.call));
		ActionItem1 item2 = new ActionItem1(0, context.getResources().getString(R.string.sendsms), context
				.getResources().getDrawable(R.drawable.smsquick));
		ActionItem1 item3 = new ActionItem1(0, context.getResources().getString(R.string.delete), context
				.getResources().getDrawable(R.drawable.deleteitem));
		ActionItem1 item4 = new ActionItem1(0, context.getResources().getString(R.string.move_to_blacklist), context
				.getResources().getDrawable(R.drawable.ic_move_blacklist));
		item4.set_is_last(true);

		mQuickAction = new QuickAction1(context, 1);
		mQuickAction.addActionItem1(item1);
		mQuickAction.addActionItem1(item2);
		mQuickAction.addActionItem1(item3);
		mQuickAction.addActionItem1(item4);
		mQuickAction
				.setOnActionItem1ClickListener(new OnActionItem1ClickListener() {

					@Override
					public void onItemClick(QuickAction1 source, int pos,
							int actionId) {
						switch (pos) {
						case 0:
							ControlPhoneAction.makeCall(activity, item.getPhoneNumber());
							break;
						case 1:
							Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
							smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
							smsIntent.setType("vnd.android-dir/mms-sms");
							smsIntent.setData(Uri.parse("sms:" + item.getPhoneNumber()));
							activity.startActivity(smsIntent);
							break;
						case 2:
							db.deleteBlackListItem(BlackListItem.ID + "=" + item.getId(), null);
							blackListItem.remove(item);
							AdapterExceptionItem.this.notifyDataSetChanged();
							break;
						case 3:
							Setting sett = db.getSetting(null);
							item.setMode(sett.getDefaultModel());
							db.updateBlackListItem(item, BlackListItem.ID + "=" + item.getId(), null);
							blackListItem.remove(item);
							AdapterExceptionItem.this.notifyDataSetChanged();
							break;
						default:
							break;
						}
					}
				});

		mQuickAction.show(view);
	}

}
